import xml2js from 'xml2js'
import sha1 from 'sha1'
export const createNoncestr = () =>{
  return Math.random().toString(36).substr(2,15)
}
export const createTimestamp = ()=> {
  return parseInt(new Date().getTime()/1000,0)+''
}
export const buildXML = (json)=>{
	var builder = new xml2js.Builder();
	return builder.buildObject(json);
}
export const parseXML = (xml, fn)=>{
	return new Promise((resolve,reject)=>{
    xml2js.parseString(xml,{trim:true, explicitArray:false, explicitRoot:false},(err,content)=>{
      if(err){
        reject(err)
      }else{
        resolve(content)
      }
    })
  })
}
export const raw = (args)=>{
  let keys = Object.keys(args);
  let newArgs = {};
  let str = '';
  keys = keys.sort();
  keys.forEach((key)=>{
    newArgs[key] =args[key]; 
  })
  for(let k in newArgs){
    str +=`&${k}=${newArgs[k]}`
  }
  return str.substr(1);
}
export const  createSignature = (options)=>{
  return sha1(raw(options));
}